#ifndef __EGLSupport_H__
#define __EGLSupport_H__


#include "U2GLESPreRequest.h"
#include "U2GLESSupport.h"
// #include "U2GLESPBuffer.h"


U2EG_NAMESPACE_BEGIN


template<class C> void removeDuplicates(C& c)
{
	std::sort(c.begin(), c.end());
	typename C::iterator p = std::unique(c.begin(), c.end());
	c.erase(p, c.end());
}


class _U2GLESShare U2EGLSupport : public U2GLESSupport
{
protected:
    /// refresh display frequency list
    void refreshConfig(void);

    EGLDisplay          mGLDisplay;
	NativeDisplayType   mNativeDisplay;

	bool                mIsExternalDisplay;
    bool                mRandr;
    /// screen size in pixel, width & height
    typedef std::pair<u2uint, u2uint>       ScreenSize;
    /// display frequency
    typedef short                           Rate;
    typedef std::pair<ScreenSize, Rate>     VideoMode;
    typedef std::vector<VideoMode>          VideoModes;
    VideoModes          mVideoModes;
    VideoMode           mOriginalMode;
    VideoMode           mCurrentMode;
    U2StringVector      mSampleLevels;

	//virtual EGLWindow* createEGLWindow( EGLSupport * support) = 0;
public:
    U2EGLSupport();
    virtual ~U2EGLSupport();

    void start(void);
    void stop(void);
    void addConfig(void);
    U2String validateConfig(void);
    void setConfigOption(const U2String &name, const U2String &value);
    virtual U2String getDisplayName (void);
    EGLDisplay getGLDisplay(void);
	void setGLDisplay(EGLDisplay val);
	EGLConfig* chooseGLConfig(const EGLint *attribList, EGLint *nElements);
    EGLBoolean getGLConfigAttrib(EGLConfig fbConfig, EGLint attribute, EGLint *value);
    void* getProcAddress(const U2String& name);
    ::EGLContext createNewContext(EGLDisplay eglDisplay, ::EGLConfig glconfig, ::EGLContext shareList) const;

    U2RenderWindow* createWindow(bool autoCreateWindow,
                               GLESRenderSystem *renderSystem,
                               const U2String& windowTitle);

//            RenderWindow* newWindow(const U2String& name,
//                                    unsigned int width, unsigned int height,
//                                    bool fullScreen,
//                                    const NameValuePairList *miscParams = 0);

    ::EGLConfig getGLConfigFromContext(::EGLContext context);
    ::EGLConfig getGLConfigFromDrawable(::EGLSurface drawable,
                                        unsigned int *w, unsigned int *h);
	::EGLConfig selectGLConfig (const EGLint* minAttribs, const EGLint *maxAttribs);
    void switchMode(void);
	virtual void switchMode(u2uint& width, u2uint& height, short& frequency) = 0;
//     virtual U2GLESPBuffer* createPBuffer(PixelComponentType format,
//                                size_t width, size_t height) = 0;
//			NativeDisplayType getNativeDisplay();
};

    
U2EG_NAMESPACE_END


#endif
